package com.it.smartoldb.repository;

import com.it.smartoldb.entity.Elderly;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 老人信息数据访问层
 */
@Repository
public interface ElderlyRepository extends JpaRepository<Elderly, Long> {
    
    /**
     * 根据用户ID查找老人信息
     */
    Elderly findByUserId(Long userId);
    
    /**
     * 根据姓名模糊查询
     */
    Page<Elderly> findByNameContaining(String name, Pageable pageable);
    
    /**
     * 根据身份证号查找
     */
    Elderly findByIdCard(String idCard);
    
    /**
     * 根据房间号查找
     */
    List<Elderly> findByRoomNumber(String roomNumber);
    
    /**
     * 根据状态查找
     */
    Page<Elderly> findByStatus(String status, Pageable pageable);
    
    /**
     * 根据护理级别查找
     */
    Page<Elderly> findByCareLevel(String careLevel, Pageable pageable);
    
    /**
     * 复合条件查询
     */
    @Query("SELECT e FROM Elderly e WHERE " +
           "(:name IS NULL OR :name = '' OR e.name LIKE %:name%) AND " +
           "(:idCard IS NULL OR :idCard = '' OR e.idCard LIKE %:idCard%) AND " +
           "(:status IS NULL OR :status = '' OR e.status = :status)")
    Page<Elderly> findByConditions(@Param("name") String name,
                                   @Param("idCard") String idCard,
                                   @Param("status") String status,
                                   Pageable pageable);
    
    /**
     * 查询所有有年龄信息的老人（用于年龄分布统计）
     */
    @Query("SELECT e.age FROM Elderly e WHERE e.age IS NOT NULL")
    List<Integer> findAllAges();
} 